﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using com.woodsoft.core;
using com.woodsoft.core.Files;
using EQ1.Objects;

namespace EQ1.Elements {
  public class Ui2DAnimation : UIClass , IUIElement<Ui2DAnimation> {
    public Ui2DAnimation ( UIDocument doc )
      : this( doc , "Ui2DAnimation" ) {
    }
    public Ui2DAnimation ( UIDocument doc , string name )
      : base( doc , name ) {
      this.CellWidth = 0;
      this.CellHeight = 0;
    }

    #region IUIElement<ButtonDrawTemplate> Members

    public void Copy ( Ui2DAnimation val ) {
      base.Copy( val );

      this.CellHeight = val.CellHeight;
      this.CellWidth = val.CellWidth;

      if( this.Cycle != null )
        this.Cycle = val.Cycle;
      if( this.Grid != null )
        this.Grid = val.Grid;
      if( this.Vertical != null )
        this.Vertical = val.Vertical;
      if( this.Frames != null )
        this.Frames = val.Frames;

    }
    public void Validate ( Ui2DAnimation val , string name ) {
      UIClass par = ( UIClass ) this.ParentNode;

      if( val.LocalName != name ) {
        Ui2DAnimation el = new Ui2DAnimation( ( UIDocument ) val.OwnerDocument , name );
        el.Copy( val );
        val = el;
      }

      if( par.GetElement( name ) != null )
        par.RemoveElement( name );

      par.AppendChild( val );
    }

    #endregion

    public int CellWidth {
      get {
        return int.Parse( this.GetElement( "CellWidth" ).InnerText );
      }
      set {
        if( this.GetElement( "CellWidth" ) == null )
          this.AddElement( "CellWidth" , value.ToString() );
        else
          this.GetElement( "CellWidth" ).InnerText = value.ToString();
      }
    }
    public int CellHeight {
      get {
        return int.Parse( this.GetElement( "CellHeight" ).InnerText );
      }
      set {
        if( this.GetElement( "CellHeight" ) == null )
          this.AddElement( "CellHeight" , value.ToString() );
        else
          this.GetElement( "CellHeight" ).InnerText = value.ToString();
      }
    }

    public bool? Cycle {
      get {
        return bool.Parse( this.GetElement( "Cycle" ).InnerText );
      }
      set {
        if( this.GetElement( "Cycle" ) == null )
          this.AddElement( "Cycle" , value.ToString() );
        else
          this.GetElement( "Cycle" ).InnerText = value.ToString();
      }
    }
    public bool? Grid {
      get {
        return bool.Parse( this.GetElement( "Grid" ).InnerText );
      }
      set {
        if( this.GetElement( "Grid" ) == null )
          this.AddElement( "Grid" , value.ToString() );
        else
          this.GetElement( "Grid" ).InnerText = value.ToString();
      }
    }
    public bool? Vertical {
      get {
        return bool.Parse( this.GetElement( "Vertical" ).InnerText );
      }
      set {
        if( this.GetElement( "Vertical" ) == null )
          this.AddElement( "Vertical" , value.ToString() );
        else
          this.GetElement( "Vertical" ).InnerText = value.ToString();
      }
    }
    public XmlNodeList Frames {
      get {
        return this.GetElementsByTagName( "Frames" );
      }
      set {
        if( value.Count != 0 ) {
          foreach( Frame nd in value ) {
            Frame frm = new Frame( ( UIDocument ) nd.OwnerDocument , "Frames" );
            frm.Texture = nd.Texture;
            frm.Location = nd.Location;
            frm.Size = nd.Size;
            frm.Hotspot = nd.Hotspot;
            frm.Duration = nd.Duration;
            frm.Shading = nd.Shading;
            frm.Specular = nd.Specular;
            this.AddFrame( frm );
          }
        }
      }
    }
    public Frame GetFrame ( string item ) {
      foreach( Frame nd in this.Frames ) {
        if( nd.item == item )
          return nd;
      }
      return null;
    }
    public void AddFrame ( Frame frame ) {
      if( frame.LocalName != "Frames" ) {
        Frame frm = new Frame( ( UIDocument ) frame.OwnerDocument , "Frames" );
        frm.Texture = frame.Texture;
        if( frame.Location != null )
          frm.Location = frame.Location;
        if( frame.Size != null )
          frm.Size = frame.Size;
        if( frame.Hotspot != null )
          frm.Hotspot = frame.Hotspot;
        frm.Duration = frame.Duration;
        frm.Specular = frame.Specular;
        frm.Shading = frame.Shading;

        frame = frm;
      }

      for( int i = this.Frames.Count - 1 ; i >= 0 ; i-- ) {
        if( ( ( Frame ) this.Frames[i] ).item == frame.item ) {
          this.ReplaceChild( frame , this.Frames[i] );
          return;
        }
      }
      this.AppendChild( frame );
    }

  }
}
